<template>
	<view class="address">
		<view class="addresslist">
			<view class="address_item" v-for="(item,index) in addressList" :key='item.id'>
				<label class="radio">
					<radio @click="AddressDefault(item)" :checked="item.is_default == 1" />
				</label>
				<view class="info">
					<view class="top">
						<text class="name">{{item.name}}</text>
						<text class="phone">{{item.phone}}</text>
					</view>
					<view class="msg">
						<text>{{item.area_name}} {{item.address}}</text>
					</view>
				</view>
				<view class="delete" @click="AddressDelete(item.id)">
					删除
				</view>
			</view>
		</view>
		<view class="noaddress">
			<view class="add_text" @click="toAdd">
				新增收货地址
			</view>
		</view>
	</view>
</template>

<script>
	import {
		AddressList,
		AddressDelete,
		AddressDefault
	} from '../../api/api.js'
	export default {
		data() {
			return {
				radioShow: 0,
				addressList: [],
				type:0
			}
		},
		onLoad(res) {
			this.type = res.type
			this.AddressList()
		},
		onShow() {
			this.AddressList()
		},
		methods: {

			AddressDefault(item) {
				if (this.type == 1) {
					let pages = getCurrentPages(); //获取所有页面栈实例列表
					let nowPage = pages[pages.length - 1]; //当前页页面实例
					let prevPage = pages[pages.length - 2]; //上一页页面实例
					prevPage.$vm.address = item; //修改上一页data里面的tagIndex 参数值
					uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
						delta: 1
					});
				}
				AddressDefault({
					address_id: item.id
				}).then(res => {
					if (res.status == 1) {
						this.AddressList()
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				})
			},
			AddressList() {
				AddressList().then(res => {
					this.addressList = res
					console.log(this.addressList)
				})
			},
			change(i) {
				this.radioShow = i
			},
			AddressDelete(id) {
				AddressDelete({
					address_id: id
				}).then(res => {
					if (res.status == 1) {
						this.AddressList()
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				})
			},
			toAdd() {
				uni.navigateTo({
					url: '/pages/addAddress/index'
				})
			}
		}
	}
</script>

<style lang="scss">
	.address {
		padding-top: 50rpx;

		.addresslist {
			.address_item {
				position: relative;
				display: flex;
				align-items: center;
				background-color: #FFFFFF;
				width: 80%;
				padding: 5%;
				margin: 0 auto;
				margin-bottom: 30rpx;
				border-radius: 10rpx;

				.info {
					margin-left: 20rpx;

					.top {
						font-size: 16px;

						.phone {
							font-size: 14px;
							color: #777777;
							margin-left: 10rpx;
						}
					}
				}

				.msg {
					margin-top: 10rpx;
					font-size: 14px;
					width: 95%;
				}

				.delete {
					position: absolute;
					right: 10px;
					top: 55px;
					font-size: 12px;
					color: #999;
				}
			}
		}

		.noaddress {
			padding-top: 50rpx;

			.add_text {
				border: 1px solid #1d3f2b;
				color: #1d3f2b;
				text-align: center;
				height: 40px;
				line-height: 40px;
				width: 50%;
				margin: 0 auto;
				border-radius: 5px;
			}
		}
	}
</style>
